/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.persistence.dao;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import edu.hpvas.persistence.entity.MedicalRecord;
import edu.hpvas.persistence.entity.Vaccine;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author raider
 */
public class VaccineDAO implements Serializable {

    public VaccineDAO() {
        this.emf = Persistence.createEntityManagerFactory("HPVAS");
    }
    private EntityManagerFactory emf;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Vaccine vaccine) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MedicalRecord idMedicalRecord = vaccine.getIdMedicalRecord();
            if (idMedicalRecord != null) {
                idMedicalRecord = em.getReference(idMedicalRecord.getClass(), idMedicalRecord.getId());
                vaccine.setIdMedicalRecord(idMedicalRecord);
            }
            em.persist(vaccine);
            if (idMedicalRecord != null) {
                idMedicalRecord.getVaccineCollection().add(vaccine);
                idMedicalRecord = em.merge(idMedicalRecord);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findVaccine(vaccine.getId()) != null) {
                System.out.println("Vaccine " + vaccine + " already exists.");
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Vaccine vaccine) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vaccine persistentVaccine = em.find(Vaccine.class, vaccine.getId());
            MedicalRecord idMedicalRecordOld = persistentVaccine.getIdMedicalRecord();
            MedicalRecord idMedicalRecordNew = vaccine.getIdMedicalRecord();
            if (idMedicalRecordNew != null) {
                idMedicalRecordNew = em.getReference(idMedicalRecordNew.getClass(), idMedicalRecordNew.getId());
                vaccine.setIdMedicalRecord(idMedicalRecordNew);
            }
            vaccine = em.merge(vaccine);
            if (idMedicalRecordOld != null && !idMedicalRecordOld.equals(idMedicalRecordNew)) {
                idMedicalRecordOld.getVaccineCollection().remove(vaccine);
                idMedicalRecordOld = em.merge(idMedicalRecordOld);
            }
            if (idMedicalRecordNew != null && !idMedicalRecordNew.equals(idMedicalRecordOld)) {
                idMedicalRecordNew.getVaccineCollection().add(vaccine);
                idMedicalRecordNew = em.merge(idMedicalRecordNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = vaccine.getId();
                if (findVaccine(id) == null) {
                    System.out.println("The vaccine with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vaccine vaccine = null;
            try {
                vaccine = em.getReference(Vaccine.class, id);
                vaccine.getId();
            } catch (EntityNotFoundException enfe) {
                System.out.println("The vaccine with id " + id + " no longer exists.");
            }
            MedicalRecord idMedicalRecord = vaccine.getIdMedicalRecord();
            if (idMedicalRecord != null) {
                idMedicalRecord.getVaccineCollection().remove(vaccine);
                idMedicalRecord = em.merge(idMedicalRecord);
            }
            em.remove(vaccine);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Vaccine> findVaccineEntities() {
        return findVaccineEntities(true, -1, -1);
    }

    public List<Vaccine> findVaccineEntities(int maxResults, int firstResult) {
        return findVaccineEntities(false, maxResults, firstResult);
    }

    private List<Vaccine> findVaccineEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Vaccine.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Vaccine findVaccine(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Vaccine.class, id);
        } finally {
            em.close();
        }
    }

    public int getVaccineCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Vaccine> rt = cq.from(Vaccine.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
